package com.supersoul.myself.entity;

import com.supersoul.myself.entity.base.BaseEntity;
import lombok.Data;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;


/**
 * 博客业务实体类, 对应数据表 tb_blog
 * @Entity
 * @Table(name = "tb_blog")
 * 上面两个注解: 是JPA 正向生成数据表 必备注解
 * @SQLDelete(sql = "update tb_blog set is_delete = 1 where id = ?")   这个注解是 逻辑删除博客, 将is_delete = 1
 */
@Data
@Entity
@Table(name = "tb_blog")
@SQLDelete(sql = "update tb_blog set is_delete = 1 where id = ?")
@Where(clause = "is_delete = 0")
public class Blog extends BaseEntity {

    // @Column name属性: 字段名,  nullable 是否为空, columnDefinition 自定义字段属性
    // @Transient  加了改注解的字段不会加入到表中

    /**
     * 文章标题, 必填
     */
    @Column(name = "title", nullable = false, length = 20)
    @NotEmpty(message = "title不能为null或''")
    @Size(max = 20, message = "title不能超过20个字")
    private String title;
    /**
     * 文章内容, 必填
     */
    @Column(name = "content", nullable = false, columnDefinition = "text")  // String 默认映射mysql varchar,  由于博客内容较多, 变更为text
    @NotEmpty(message = "content不能为null或''")
    private String content;

    /**
     * 文章内容render, 必填
     */
    @Column(name = "render", nullable = false, columnDefinition = "text")  // String 默认映射mysql varchar,  由于博客内容较多, 变更为text
    @NotEmpty(message = "render不能为null或''")
    private String render;

    // 能让用户调整文章顺序
    /**
     * 文章自定义顺序
     */
    @Column(name = "content_order", columnDefinition = "int(10) comment '文章自定义顺序' ")
    private Integer contentOrder;

    // 个人隐私字段,  我的博客是公开的,还是私密的, 我不想发布的时候, 不会让你们看到
    /**
     * 1.公开  2.私密, 必填
     */
    @Column(name = "permission_state", nullable = false, columnDefinition = "varchar(255) comment '1.公开  2.私密' ")
    private Integer permissionState;
    /**
     * 1.草稿  2. 已发布, 必填
     */
    @Column(name = "publish_state", nullable = false,  columnDefinition = "varchar(255) comment '1.草稿  2.已发布' ")
    @NotNull(message = "publishState不能为null")
    private Integer publishState;

    // 博客分类
    /**
     * 分类id, 必填
     */
    @Column(name = "cg_id", nullable = false, length = 64, columnDefinition = "varchar(64) comment '分类id' ")
    @NotEmpty(message = "cgId不能为null或''")
    @Size(max = 64, message = "cgId不能超过64个字")
    private String cgId;


    /**
     * 分类id, 必填
     */
    @Column(name = "likes", nullable = false, length = 11, columnDefinition = "int(11) comment '点赞数量' ")
    private Integer likes = 0;


    /**
     * 分类id, 必填
     */
    @Column(name = "views", nullable = false, length = 11, columnDefinition = "int(11) comment '浏览数量' ")
    private Integer views = 0;


    // 记录操作数据的人的信息,方便追踪
    // 操作人信息
    @Column(name = "creator_id", nullable = false, length = 64)
    @Size(max = 64, message = "creatorId不能超过64个字")
    private String creatorId;

    @Column(name = "creator_name", nullable = false, length = 20)
    @Size(max = 20, message = "creatorName不能超过20个字")
    public String creatorName;

    // 更新人信息
    @Column(name = "updator_id", length = 64)
    @Size(max = 64, message = "updatorId不能超过64个字")
    public String updatorId;

    @Column(name = "updator_name", length = 20)
    @Size(max = 20, message = "updatorName不能超过20个字")
    public String updatorName;
}
